<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\facade\Config;

class Epay extends Backend
{
    protected $noNeedRight = ['upload'];

    /**
     * 上传本地证书
     * @return void
     */
    public function upload()
    {
        Config::set(['default_return_type' => 'json'], 'app');

        $certname = $this->request->post('certname', '');
        $certPathArr = [
            'cert_client'         => 'apiclient_cert', //微信支付api
            'cert_key'            => 'apiclient_key', //微信支付api
            'app_cert_public_key' => 'appCertPublicKey',//应用公钥证书路径
            'alipay_root_cert'    => 'alipayRootCert', //支付宝根证书路径
            'ali_public_key'      => 'alipayCertPublicKey', //支付宝公钥证书路径
        ];
        if (!isset($certPathArr[$certname])) {
            $this->error("证书错误");
        }
        $url = $certPathArr[$certname];
        $file = $this->request->file('file');
        if (!$file) {
            $this->error("未上传文件");
        }

        $_path = 'addons/epay/certs';
        $path = $this->app->getRootPath().$_path;
        $config = Config::get('filesystem');
        $config['disks']['epay']  = [
            'type' => 'local',
            'root'       => $path
        ];
        Config::set($config,'filesystem');

        $savename = \think\facade\Filesystem::disk('epay')->putFile('', $file, function ($file) use ($url) {
            //重命名
            return $url;
        });
        $_url = '/'.$_path.'/'.$savename;
        $this->success(__('上传成功'), '', ['url' => $_url]);
    }
}
